package cc.lishixi.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

public class MyWorkDemo {
    JFrame frame = new JFrame();
    JButton start = new JButton("Start");
    JButton pause = new JButton("Pause");
    JButton stop = new JButton("Stop");
    JButton resume = new JButton("Resume");
    SwingWorker<Boolean, Integer> swingWorker;
    final JProgressBar progressBar = new JProgressBar(1, 100);
    Boolean suspend = false;

    public void createAndShowUI() {

        frame.setSize(new Dimension(300, 100));
        progressBar.setStringPainted(true);

        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (swingWorker != null && !swingWorker.isDone()) {
                    JOptionPane.showMessageDialog(null, "有任务正在进行中。");
                    return;
                }
                swingWorker = new SwingWorker<Boolean, Integer>() {
                    @Override
                    protected Boolean doInBackground() throws Exception {
                        for (int i = 0; i < progressBar.getMaximum(); i++) {
                            while (suspend) {
                                Thread.sleep(100);
                            }
                            publish(i);
                            Thread.sleep(100);
                        }
                        return Boolean.TRUE;
                    }

                    @Override
                    protected void process(List<Integer> chunks) {
                        Integer value = chunks.get(0);
                        progressBar.setValue(value);
                        super.process(chunks);
                    }

                    @Override
                    protected void done() {
                        progressBar.setValue(100);
                        super.done();
                    }
                };
                swingWorker.execute();
            }
        });

        pause.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                synchronized (swingWorker) {
                    suspend = true;
                }
            }
        });

        stop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                stopTask();
            }
        });

        resume.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                synchronized (swingWorker) {
                    suspend = false;
                }
            }
        });

        JPanel topPanel = new JPanel();
        topPanel.add(start);
        topPanel.add(pause);
        topPanel.add(resume);
        topPanel.add(stop);

        frame.add(topPanel, BorderLayout.PAGE_START);
        JPanel bottomPanel = new JPanel();

        bottomPanel.add(progressBar);
        frame.add(bottomPanel, BorderLayout.PAGE_END);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                super.windowClosed(e);
                stopTask();
            }
        });
        frame.setVisible(true);
    }

    private void stopTask() {
        swingWorker.cancel(true);
    }

    public static void main(String[] args) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyWorkDemo myWorkDemo = new MyWorkDemo();
                myWorkDemo.createAndShowUI();
            }
        });
    }
}
